<?php

namespace App\Http\Controllers\v1;


use App\Models\Area;
use App\Models\Family;
use App\Models\Project;
use App\Models\SmsCode;
use App\Models\Team;
use App\Models\User;
use App\Models\UserCollection;
use App\Models\UserMessage;
use App\Models\UserProject;
use App\Models\UserTeam;
use App\Models\Volunteer;
use App\Models\VolunteerGoodAt;
use EasyWeChat\Core\Exception;
use Illuminate\Http\Request;
use App\Libs\IdCard;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Hashids\Hashids;
use anlutro\LaravelSettings\Facade AS Setting;

class UserProjectController extends Controller
{

    public function index(Request $request)
    {
        $rules = ['status'=>'in:1,2,3'];
        $validator = Validator::make(Input::all(), $rules);
        if($validator->fails())
        {
            $errors = $validator->errors();
            $this->setStatus(10001);
            $this->setMsg(implode(' ', $errors->all()));

            return $this->response();
        }

        $status = $request->input('status', -1);
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();

        $nodes = UserProject::where('user_id', $userInfo->id);
        switch($status)
        {
            //待录用
            case 1:
                $nodes = $nodes->where('verify_status', 0);
                break;
            //已录用 未结束
            case 2:
                $pid = Project::selectRaw("group_concat(id) as ids")->where('end', '>', date('Y-m-d H:i:s'))->whereRaw("id in (select project_id from user_project where user_id={$userInfo->id})")->first();
                $nodes = $nodes->where('verify_status', 1)->whereIn('project_id', explode(',', $pid->ids));
                break;

            //已录用,已结束|已拒绝
            case 3:
                $pid = Project::selectRaw("group_concat(id) as ids")->where('end', '<', date('Y-m-d H:i:s'))->whereRaw("id in (select project_id from user_project where user_id={$userInfo->id})")->first();
                $ids = empty($pid->ids)? 0:$pid->ids;
                $nodes = $nodes->whereRaw("((verify_status=2) or (verify_status=1 and project_id in ({$ids})))");
                break;
        }

        $nodes = $nodes->orderBy('id', 'desc');
        $userProject = $nodes->get()->toArray();

        $projectIds = array_map(function($item){
            return $item['project_id'];
        }, $userProject);

        foreach($userProject as $value)
        {

            $statusList[$value['project_id']] = $value['verify_status'];
        }

        $lists = Project::with(['team','category', 'serviceObj'])->whereIn('id', $projectIds)->get();
        $projectList = [];
        foreach($lists as $value)
        {
            $tmp = $value->getAllInfo();
            $projectList[$value->id] = $tmp;
        }

        $list = [];
        foreach($projectIds as $item)
        {
            if(isset($projectList[$item]))
            {
                $tmp = $projectList[$item];
                $tmp['join_status'] = $statusList[$tmp['id']];
                $list[] = $tmp;
            }
        }

        $data['list'] = $list;
        $this->setData($data);

        return $this->response();
    }

    public function reward(Request $request)
    {
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();

        $nodes = UserProject::with('project')->where('user_id', $userInfo->id);
        $nodes = $nodes->where('reward_time', '>', 0);
        $nodes = $nodes->orderBy('id', 'desc');
        $nodes = $nodes->get();

        $joinCount = count($nodes);

        $list = [];
        $rewardTime = 0;
        foreach($nodes as $value)
        {
            $tmp = $value->project->getAllInfo();
            $tmp['my_reward_time'] = $value->reward_time;
            $rewardTime += $value->reward_time;

            $list[] = $tmp;
        }

        $data['reward_time'] = $rewardTime;
        $data['join_project_count'] = $joinCount;
        $data['project'] = $list;

        $this->setData($data);
        return $this->response();
    }

    public function applyProject(Request $request)
    {
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();

        $nodes = UserProject::with('project')->where('user_id', $userInfo->id);
        $nodes = $nodes->where('verify_status', 1);
        $nodes = $nodes->orderBy('id', 'desc');
        $nodes = $nodes->get();

        $list = [];
        foreach($nodes as $value)
        {
            $tmp = $value->project->getAllInfo();
            $list[] = $tmp;
        }

        $data['list'] = $list;
        $this->setData($data);
        return $this->response();
    }

}
